<?php

namespace core;
// require 'common.php';
// define('CONFIG', require '../config.php');

// 路由类
class Router
{
    public static function parse()
    {
        //http://tmvc.io/admin.php/controller/action/k1/k2/k3....
        // http://tmvc.io/admin.php/index/index/a/1/b/2....
        $controller = CONFIG['app']['default_controller'];
        $action = CONFIG['app']['default_action'];
        $params = [];
        //$_SERVER['PATH_INFO']
        if(array_key_exists('PATH_INFO', $_SERVER) && $_SERVER['PATH_INFO'] !== '/'){
            //explode() 将字符串转换数组
            // echo $_SERVER['PATH_INFO'];
            $pathinfo = array_values(array_filter(explode('/', $_SERVER['PATH_INFO'])));
            if (count($pathinfo) >= 2) {
                $controller = array_shift($pathinfo);
                $action = array_shift($pathinfo);
                $params = $pathinfo;
            } else{
                $controller = array_shift($pathinfo);
            } 
            
        }
        // [控制器,方法,参数方法]
        return [$controller,$action,$params];
    }
}

//http://localhost:3000/core/Router.php/index/add/a/1/b/2/c/3
// d(Router::parse());